package com.dylan.历史.领扣;

/**
 * @Auther: ankang
 * @Date: 2019/2/22 20:35
 * @Description:
 */
public class $0059螺旋矩阵二 {

    public static void main(String[] args) {
        new $0059螺旋矩阵二.Solution().generateMatrix(4);
    }

    static class Solution {
        public int[][] generateMatrix(int n) {
            int[][] result = new int[n][n];
            generateMatrixDo(result, 0, n - 1, 0, n - 1, 0, 0, 1);
            return result;
        }

        public void generateMatrixDo(int[][] result, int left, int right, int up, int down, int row, int column, int start) {
            if (left > right || up > down) {
                return;
            }
            if (row == up && column == left) {
                for (int i = left; i <= right; i++) {
                    result[up][i] = start++;
                }
                up++;
                row++;
                column = right;
            } else if (row == up && column == right) {
                for (int i = up; i <= down; i++) {
                    result[i][right] = start++;
                }
                right--;
                column--;
                row = down;
            } else if (row == down && column == right) {
                for (int i = right; i >= left; i--) {
                    result[down][i] = start++;
                }
                down--;
                row--;
                column = left;
            } else if (row == down && column == left) {
                for (int i = down; i >= up; i--) {
                    result[i][left] = start++;
                }
                left++;
                column++;
                row = up;
            }
            generateMatrixDo(result, left, right, up, down, row, column, start);
        }
    }
}
